﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Types;

namespace Client
{
    public partial class SubscribeForm : Form
    {
        public delegate void ErrorMessageDelegate(string message);
        public delegate void CloseFormDelegate(UserType user);

        public SubscribeForm()
        {
            InitializeComponent();
            ClientManager.Instance.showErrorMessage += new ClientManager.ShowErrorMessage(this.ShowErrorMessage);
        }

        private void ShowErrorMessage(string message)
        {
            if (InvokeRequired)
            {
                Invoke(new ErrorMessageDelegate(this.ShowErrorMessage), message);
            }
            else
            {
                // clear the fildes
                MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            try
            {
                ClientManager.Instance.SubscribeNewUser(usernameTextBox.Text, passwordTextBox.Text, firstNameTextBox.Text, lastNameTextBox.Text, idTextBox.Text, int.Parse(educationYearTextBox.Text));
            }
            catch (ConnectionException ex)
            {
                MessageBox.Show(ex.Message + "\nProgram will exit", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                System.Environment.Exit(System.Environment.ExitCode);
            }
            this.Close();
        }

        private void usernameTextBox_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (!Regex.IsMatch(usernameTextBox.Text, @"^[a-zA-Z'./s]{1,10}$"))
                {
                    throw (new ValidationException(usernameTextBox, "Username should be up to 10 characters"));
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message);
                ex.BadTextBox.Focus();
                ex.BadTextBox.Clear();
            }
        }

        private void passwordTextBox_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (!Regex.IsMatch(passwordTextBox.Text, @"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{3,10}$"))
                {
                    throw (new ValidationException(passwordTextBox, "Password should be at least 4 characters, no more than 8 characters, and must include at least one upper case letter, one lower case letter, and one numeric digit"));
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message);
                ex.BadTextBox.Focus();
                ex.BadTextBox.Clear();
            }
        }

        private void firstNameTextBox_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (!Regex.IsMatch(firstNameTextBox.Text, @"^[a-zA-Z'./s]{1,50}$"))
                {
                    throw (new ValidationException(firstNameTextBox, "First name should be up to 50 characters"));
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message);
                ex.BadTextBox.Focus();
                ex.BadTextBox.Clear();
            }
        }

        private void lastNameTextBox_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (!Regex.IsMatch(lastNameTextBox.Text, @"^[a-zA-Z'./s]{1,50}$"))
                {
                    throw (new ValidationException(lastNameTextBox, "Last name should be up to 50 charactesr"));
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message);
                ex.BadTextBox.Focus();
                ex.BadTextBox.Clear();
            }
        }

        private void idTextBox_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (!Regex.IsMatch(idTextBox.Text, @"^[a-zA-Z0-9'./s]{1,50}$"))
                {
                    throw (new ValidationException(idTextBox, "Id should be up to 10 charactesr"));
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message);
                ex.BadTextBox.Focus();
                ex.BadTextBox.Clear();
            }
        }

        private void educationYearTextBox_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (!Regex.IsMatch(educationYearTextBox.Text, @"^[1-3]{1}$"))
                {
                    throw (new ValidationException(educationYearTextBox, "Education year should be an integer between 1 to 3"));
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message);
                ex.BadTextBox.Focus();
                ex.BadTextBox.Clear();
            }
        }
    }
}
